<script>
	import {
		getStorage,
		removeStorage
	} from '@/util/auth.js';
	import {
		getWxLoginCode
	} from '@/util/user.js'
	export default {
		onLaunch: function() {
			uni.loadFontFace({
				source: 'url("https://job-supermarket-1306862033.cos.ap-chengdu.myqcloud.com/uniapp/%E6%96%B9%E6%AD%A3%E6%B1%89%E7%9C%9F%E5%B9%BF%E6%A0%87%E7%AE%80%E4%BD%93.TTF")',
				family: 'FangZhengHanZhenGuangBiaoJianTi',
				global: true,
			})
			//监听路由跳转
			let _this = this;
			uni.addInterceptor('navigateTo', {
				invoke(e) {
					return _this.watchRouter(e);
				}
			})
			uni.addInterceptor('redirectTo', {
				invoke(e) {
					return _this.watchRouter(e);
				}
			})
			uni.addInterceptor('reLaunch', {
				invoke(e) {
					return _this.watchRouter(e);
				}
			})
			//自动登录
			if (!this.$store.getters.token) {
				getWxLoginCode().then((code) => {
					uni.showLoading({
						title: '加载中',
						mask: true,
					})
					this.$store.dispatch('user/getWxLoginCode', code).catch(() => {
						uni.showModal({
							title: '提示',
							content: '获取信息失败，请尝试重新进入小程序，否则功能无法正常使用',
						})
					})
				})
			} else {
				this.$store.dispatch('user/getUser');
			}
			this.$store.dispatch('position/getPosition')
		},
		methods: {
			watchRouter(e) {
				let url = e.url;
				const index = url.indexOf('?');
				url = index == -1 ? url : url.slice(0, index);
				const hasToken = getStorage('token');
				const user = getStorage('user');
				const whiteList = [
					'/page_pack/user/info',
					'/page_pack/motion_video',
					'/page_pack/market/list', '/page_pack/entrepreneurship/enter',
					'/page_pack/entrepreneurship/ship_list', '/page_pack/entrepreneurship/village',
					'/page_pack/motion_video/index', '/page_pack/entrepreneurship/map', '/page_pack/special/enter',
					'/page_pack/market/list', '/page_pack/market/detail', '/page_pack/special/list',
					'/page_pack/special/detail', '/page_pack/food/list', '/page_pack/food/detail',
					'/page_pack/landscape/list', '/page_pack/landscape/detail',
					'/page_pack/entrepreneurship/recruit_list', '/page_pack/motion_video/list',
					'/page_pack/motion_video/detail', '/page_pack/entrepreneurship/bussiness'
				];
				const condition = user && user.phone && hasToken;
				if (user && user.phone && hasToken) {
					return true
				} else {
					if (whiteList.includes(url)) {
						return true
					} else {
						uni.showModal({
							title: '提示',
							content: '需要先登录！',
							cancelText: '先逛逛',
							confirmText: '去登录',
							success(res) {
								if (res.confirm) {
									uni.navigateTo({
										url: '/page_pack/user/info'
									})
								}
							}
						})
					}
				}
				return false;
			}
		},
	}
</script>

<style lang="scss">
	@import url("@/static/iconfont/iconfont.css");

	.FZHZGBJ {
		font-family: 'FangZhengHanZhenGuangBiaoJianTi';
	}

	button {
		padding: 0;
		background: none;
		line-height: normal !important;
	}

	button::after {
		border: none !important;
	}

	view,
	text,
	navigator,
	input,
	picker,
	button,
	scroll-view,
	textarea,
	label,
	image {
		box-sizing: border-box;
	}

	.empty {
		width: 100%;
		text-align: center;
		color: #313131;
		font-size: 28rpx;
		margin: 30rpx auto;
	}

	page {
		min-height: calc(100vh - var(--window-bottom) - var(--window-top));
		background: $background-color;
	}

	.page {
		width: 100%;
		min-height: calc(100vh - var(--window-bottom) - var(--window-top));
	}

	.input-placeholder {
		color: #686868;
		font-weight: normal;
	}

	.btn {
		width: 300rpx;
		height: 80rpx;
		border-radius: 20px;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.btn.normal {
		color: #fff;
		background: linear-gradient(to right, #1D9266, #CCCD31);
	}

	.btn.cancel {
		color: $theme-color;
		background: #fff;
		border: 1px solid $theme-color;
	}

	.btn.disabled {
		color: #fff;
		background: #999;
		border: none;
	}

	image {
		display: block;
	}
</style>